<template>
  <!-- 用户导入对话框 -->
  <el-dialog
    class="dialogClass"
    :title="upload.title"
    :visible.sync="upload.open"
    width="420px"
    append-to-body
  >
    <template slot="title">
      <div class="modalTitle">
        <img
          src="@/assets/bluelogo/drawerTitle.png"
          alt=""
          class="titleLogo"
        />{{ upload.title }}
      </div>
    </template>
    <el-upload
      c
      ref="upload"
      :limit="1"
      accept=".xlsx, .xls"
      :headers="upload.headers"
      :action="upload.url + '?updateSupport=' + upload.updateSupport"
      :disabled="upload.isUploading"
      :on-progress="handleFileUploadProgress"
      :on-success="handleFileSuccess"
      :auto-upload="false"
      drag
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip text-center" slot="tip">
        <div class="el-upload__tip" slot="tip">
          <!-- <el-checkbox v-model="upload.updateSupport" />
          是否更新已经存在的用户数据 -->
        </div>
        <span>仅允许导入xls、xlsx格式文件。</span>
        <el-link
          type="primary"
          :underline="false"
          style="font-size: 12px; vertical-align: baseline"
          @click="importTemplate"
          >下载模板</el-link
        >
      </div>
    </el-upload>
    <importModal ref="importModal" :personList="personList"></importModal>
    <div slot="footer" class="dialog-footer">
      <el-button @click="upload.open = false" class="resetButton"
        >取 消</el-button
      >
      <el-button @click="submitFileForm" class="searchButton">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
import importModal from "./importModal.vue";
import { DOWNLOADTEMPLE } from "../../../../utils/variable";
export default {
  name: "UserUpload",
  components: { importModal },
  data() {
    return {
      deptOptions: [],
      //返回的人员列表
      personList: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/supervision/task/importData",
      },
    };
  },
  mounted() {},
  methods: {
    /** 下载模板操作 */
    importTemplate() {
      let a = document.createElement('a');
      let evt = document.createEvent('MouseEvents');
      a.download = '/downLoadTemplate/核查任务模板.xlsx';
      a.href = '/downLoadTemplate/核查任务模板.xlsx';
      evt.initEvent('click',true,true);
      a.dispatchEvent(evt);
      window.URL.revokeObjectURL(a.href);

      // window.location.href =
      //   "http://" + DOWNLOADTEMPLE + "/downLoadTemplate/核查任务模板.xlsx";
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if (response.data) {
        response.data.forEach((element) => {
          //每条属性增加showFlag属性，默认只暂时名称，判决书号和执行案号
          element.showFlag = false;
          //每条属性增加buttonContent属性，默认按钮显示保存
          element.buttonContent = "保存";
          //每条属性增加isSave属性，默认每一条都可以编辑
          element.isSave = false;
          element.oucodeList = [
            element.provinceCode,
            element.cityCode,
            element.countyCode,
          ];
        });
      }
      this.personList = response.data;
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style scoped lang="scss">
@import "../../../../assets/styles/dialog.scss"; //模态框样式
.searchButton {
  width: 81px;
  border: 0px;
  height: 39px;
  background: #598af9 !important;
  border-radius: 3px;
  font-size: 16px;
  margin-right: 10px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  letter-spacing: 0;
  color: #fefbfb;
  line-height: 10px;
  letter-spacing: 2px;
}
.dialogClass{
 ::v-deep .el-dialog{
  top: 100px;
  }
}
</style>
